package lx.utils.demo;

import lx.utils.http.TextRequest.BaseRequest;
import lx.utils.http.TextRequest.DataHull;
import lx.utils.http.TextRequest.BaseRequest.DataParser;
import lx.utils.http.TextRequest.BaseRequest.TaskCallback;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.lx.utils.R;

final public class ActivityJsonRequestDemo extends Activity {

    TextView mTvDebugInfo;
    Button mBtnRequestWeather;
    Button mBtnRequestDouban;

    ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_json_demo);
        mTvDebugInfo = (TextView) findViewById(R.id.arw_tv_debug_info);
        mBtnRequestWeather = (Button) findViewById(R.id.arw_btn_request_weather);
        mBtnRequestDouban = (Button) findViewById(R.id.arw_btn_request_douban);

        mTvDebugInfo.setMovementMethod(ScrollingMovementMethod.getInstance());

        mBtnRequestWeather.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                requestWeather();
            }
        });

        mBtnRequestDouban.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                requestDouban();
            }
        });
    }

    private void requestWeather() {
        final String JINING_WEATHER_URL =
                "http://www.weather.com.cn/data/cityinfo/101120701.html";

        BaseRequest request = new BaseRequest.Builder(ActivityJsonRequestDemo.this, JINING_WEATHER_URL)
                .setTaskCallback(new TaskCallback() {
                    @Override
                    public void onResponse(DataHull datahull) {
                        Log.d("liuxu", "activity, get response *** \n" + datahull);
                        mTvDebugInfo.setText(datahull.toString());
                        showProgressDlg(false);
                    }
                })
                .setDataParser(new DataParser() {
                    @Override
                    public Object parseData(String data) {
                        Log.d("liuxu", "activity, parse data");
                        WeatherBaseModel model = JSON.parseObject(data,
                                new TypeReference<WeatherBaseModel>() {
                                });
                        if (model != null && model.getWeatherinfo() == null) {
                            model = null;
                        }
                        return model;
                    }
                })
                .create();

        mTvDebugInfo.setText("...");
        showProgressDlg(true);
        request.execute();
    }

    private void requestDouban() {
        final String DOUBAN_BOOK =
                "http://api.douban.com/v2/book/1220562";

        BaseRequest request = new BaseRequest.Builder(ActivityJsonRequestDemo.this, DOUBAN_BOOK)
                .setCacheEnabled(true)
                .setCacheTime(10000)
                // use header to fake as Mozilla browser, since douban will
                // block the request if not from browser.
                .setHeader(
                        "User-Agent",
                        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; .NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)")
                .setTaskCallback(new TaskCallback() {
                    @Override
                    public void onResponse(DataHull datahull) {
                        Log.d("liuxu", "activity, get response *** \n" + datahull);
                        mTvDebugInfo.setText(datahull.toString());
                        showProgressDlg(false);
                    }
                })
                .create();

        mTvDebugInfo.setText("...");
        showProgressDlg(true);
        request.execute();
    }

    private void showProgressDlg(boolean show) {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setIndeterminate(true);
            mProgressDialog.setCancelable(false);
            mProgressDialog.setMessage("Loading ...");
        }
        if (show) {
            mProgressDialog.show();
        } else {
            mProgressDialog.dismiss();
        }
    }

}
